package com.mvp.shq.washer.ui.shop.productDetail;

import com.mvp.shq.washer.entity.User;
import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.entity.shop.CollectResponse;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/10.
 */

public class Presenter extends ProductDetailConstract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    void getProductInfo(String productId) {
        String userId = null;
        User user = SpUtil.getUser();
        if (user != null){
            userId = user.getId();
        }
        model.getProductDetail(userId,productId)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<Product>(view) {
                    @Override
                    protected void onSuccees(BaseResult<Product> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getProductInfoS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void getProductInfoBySpu(String productId) {
        String userId = null;
        User user = SpUtil.getUser();
        if (user != null){
            userId = user.getId();
        }
        model.getProductDetailBySpu(userId,productId)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<Product>(view) {
                    @Override
                    protected void onSuccees(BaseResult<Product> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getProductInfoS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void getProductByAttr(String productId, String attrs) {
        model.getProductByAttr(productId,attrs)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<Product>(view) {
                    @Override
                    protected void onSuccees(BaseResult<Product> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getProductByAttrS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void collectProduct(String productId) {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        List<CollectResponse> list = new ArrayList<>();
        CollectResponse response = new CollectResponse(productId,userId);
        list.add(response);
        model.collectProduct(token,userId,list)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.collectProductS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void delCollectProduct(String productId) {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        model.delProduct(token,userId,productId)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.delCollectProductS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }
}
